#include "gui/homescreen.h"
#include "gui/separatorwidget.h"
#include "core/constants.h"
#include "core/filehandler.h"

#include <QDebug>

void HomeScreen::setOpenFile(QString filePath)
{
    QString text = "Currently working on: %1";
    this->subHeader->setText(text.arg(filePath));
    this->newFileButton->hide();
    this->openFileButton->hide();
    this->closeFileButton->show();
}

void HomeScreen::clearOpenFile()
{
    this->subHeader->setText("No file is currently open.");
    this->newFileButton->show();
    this->openFileButton->show();
    this->closeFileButton->hide();
}

void HomeScreen::onCreateCmdLinkClicked()
{
    QString filePath = QFileDialog::getSaveFileName(this, QString("Create a %1 file").arg(APPNAME_SHORT));

    if (filePath != "") {
        setOpenFile(filePath);
        qDebug() << "HomeScreen: Emitting fileOpened: " << filePath;
        FileHandler::openFileAt(filePath, FileHandler::InitDatabase);
        emit fileOpened(filePath);
    }
}

void HomeScreen::onOpenCmdLinkClicked()
{
    QString filePath = QFileDialog::getOpenFileName(this, QString("Open %1 file").arg(APPNAME_SHORT));

    if (filePath != "") {
        setOpenFile(filePath);
        qDebug() << "HomeScreen: Emitting fileOpened: " << filePath;
        FileHandler::openFileAt(filePath, FileHandler::AssumeInitialized);
        emit fileOpened(filePath);
    }
}

void HomeScreen::onCloseCmdLinkClicked()
{
    FileHandler::closeFile();
    this->clearOpenFile();
}

void HomeScreen::initUiComponents()
{
    mainLayout = new QVBoxLayout;

    header = new QLabel(QString("Welcome to the %1.").arg(APPNAME));
    header->setWordWrap(true);
    QFont headerFont = header->font();
    headerFont.setPointSize(headerFont.pointSize() + 6);

    header->setFont(headerFont);
    mainLayout->addWidget(header);

    subHeader = new QLabel(QString("No %1 file is currently open.").arg(APPNAME_SHORT));

    mainLayout->addWidget(subHeader);

    newFileButton = new QCommandLinkButton;
    newFileButton->setText("Create a new file");
    newFileButton->setDescription(
                QString("Start with a new, empty %1 file").arg(APPNAME_SHORT));
    mainLayout->addWidget(newFileButton);

    openFileButton = new QCommandLinkButton;
    openFileButton->setText("Open an existing file");
    openFileButton->setDescription(
                QString("Load an existing %1 file to work with "
                        "your current data").arg(APPNAME_SHORT));
    mainLayout->addWidget(openFileButton);

    closeFileButton = new QCommandLinkButton;
    closeFileButton->setText("Close current file");
    closeFileButton->setDescription("Saves all changes and closes the current file.");
    mainLayout->addWidget(closeFileButton);

    mainLayout->addStretch();
    this->setLayout(mainLayout);
}

void HomeScreen::initSignalsAndSlots()
{
    connect(this->newFileButton, SIGNAL(clicked()),
            this, SLOT(onCreateCmdLinkClicked()));
    connect(this->openFileButton, SIGNAL(clicked()),
            this, SLOT(onOpenCmdLinkClicked()));
    connect(this->closeFileButton, SIGNAL(clicked()),
            this, SLOT(onCloseCmdLinkClicked()));
}

HomeScreen::HomeScreen(QWidget *parent) :
    QWidget(parent)
{
    this->initUiComponents();
    this->clearOpenFile();
    this->initSignalsAndSlots();
}
